/*
 * Copyright 2015-2019 Evun Technology. 
 * 
 * This software is the confidential and proprietary information of
 * Evun Technology. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with evun.cn.
 */
package cn.evun.sweet.demo.model;

import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.GroupSequence;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import cn.evun.sweet.core.validation.GlobalUnique;
import cn.evun.sweet.core.validation.SqlCheck;
import cn.evun.sweet.core.validation.TenantUnique;
import cn.evun.sweet.core.validation.ValidateStep;

/**
 * 一般情况下，可以在DO上直接添加验证注解，也可以使用新的DTO携带验证注解完成验证工作。
 *
 * @author yangw
 * @since 1.0.0
 */
@Table(schema="u", name="sweet_demo_user")
@GroupSequence({ValidateStep.First.class, ValidateStep.Second.class, ValidateStep.Third.class, UserDTO.class})
public class UserDTO {
	
	@NotEmpty(message = "{user.code.null}", groups = {ValidateStep.First.class})  
	@Pattern(regexp = "(\\w){5,20}", message = "{user.code.illegal}", groups = {ValidateStep.First.class})
	@TenantUnique(model="user", property="userCode", tenantAlia="userTenantId", message="{code was used by another user}",
			conditions={"userIsdel"}, conditionValues={"0"}, groups = {ValidateStep.Second.class}) //用户编号租户内唯一
	@SqlCheck(sqlid="backend.validate.usercode", SpEL="#result.get(0)<1",
			message="{code was used by another user}", groups = {ValidateStep.Third.class}) //用户编号编辑时租户内唯一
	public String  userCode;   
	
	@Length(min = 1, max = 20, message = "{user.name.illegal}", groups = {ValidateStep.First.class}) 
	@NotEmpty(message = "{user.name.null}", groups = {ValidateStep.First.class})       
	public String  userName;
		
	@Pattern(regexp = "^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$", message = "{user.mobile.illegal}", groups = {ValidateStep.First.class})	
	@NotEmpty(message = "{user.mobile.null}", groups = {ValidateStep.First.class})  
	@GlobalUnique(model="user", property="userMobile", message="{mobile number was used}",
			conditions={"userIsdel"}, conditionValues={"0"}, groups = {ValidateStep.Second.class}) //手机号码全局唯一
	public String  userMobile; 	
	
	@NotNull(message = "{user.org.null}", groups = {ValidateStep.First.class}) 
	public Long    userOrgId;

	@Id
	public Long    userId;
	public Integer userRole;	
	public String  userSign;
	public Date    userModifiedon;  
	public Boolean userIsdel;
	
	public String getUserCode() {
		return userCode;
	}
	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserMobile() {
		return userMobile;
	}
	public void setUserMobile(String userMobile) {
		this.userMobile = userMobile;
	}
	public String getUserSign() {
		return userSign;
	}
	public void setUserSign(String userSign) {
		this.userSign = userSign;
	}
	public Integer getUserRole() {
		return userRole;
	}
	public void setUserRole(Integer userRole) {
		this.userRole = userRole;
	}
	public Long getUserOrgId() {
		return userOrgId;
	}
	public void setUserOrgId(Long userOrgId) {
		this.userOrgId = userOrgId;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public Date getUserModifiedon() {
		return userModifiedon;
	}
	public void setUserModifiedon(Date userModifiedon) {
		this.userModifiedon = userModifiedon;
		if(this.userRole == null){
			this.setUserRole(3);
		}
	}
	public Boolean getUserIsdel() {
		return userIsdel;
	}
	public void setUserIsdel(Boolean userIsdel) {
		this.userIsdel = userIsdel;
	}	
}
